package xiaoqi.qingyue.api.basic.user.controller;

import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import xiaoqi.qingyue.api.basic.user.model.entity.User;
import xiaoqi.qingyue.api.common.Result;
import xiaoqi.qingyue.api.basic.user.model.dto.UserDto;
import xiaoqi.qingyue.api.basic.user.model.query.LoginQuery;
import xiaoqi.qingyue.api.basic.user.service.UserService;

import java.util.List;

import static xiaoqi.qingyue.api.common.Instance.SYSTEM_EXCEPTION;

/**
 * @author 92110
 */
@CrossOrigin
@RestController
@RequestMapping("/user")
@RequiredArgsConstructor
public class UserController {

    private final UserService userService;
    private static final Logger logger = LoggerFactory.getLogger(UserController.class);

    @PostMapping("/login")
    public Result<String> login(@RequestBody LoginQuery loginQuery) {
        try {
            String login = userService.login(loginQuery);
            if (login == null) {
                return Result.error(401, "用户名或密码错误！");
            }
            return Result.success("登录成功", login);
        } catch (Exception e) {
            logger.error("用户登录异常:{}", e.getMessage());
            return Result.error(SYSTEM_EXCEPTION);
        }
    }

    @PostMapping("/insert")
    public Result<String> insertUser(@RequestBody UserDto userDto) {
        try {
            userService.insertUser(userDto);
            return Result.success("添加成功");
        } catch (Exception e) {
            logger.error("用户添加异常:{}", e.getMessage());
            return Result.error(SYSTEM_EXCEPTION);
        }
    }

    @GetMapping("/info/{username}")
    public Result<UserDto> getUserInfo(@PathVariable String username) {
        try {
            UserDto userInfo = userService.getUserInfo(username);
            return Result.success("请求成功!", userInfo);
        } catch (Exception e) {
            logger.error("获取用户信息异常:{}", e.getMessage());
            return Result.error(SYSTEM_EXCEPTION);
        }
    }

    @GetMapping("/list")
    public Result<List<UserDto>> getUserList() {
        try {
            List<UserDto> userList = userService.getUserList();
            return Result.success("请求成功!", userList);
        } catch (Exception e) {
            logger.error("获取用户列表异常:{}", e.getMessage());
            return Result.error(SYSTEM_EXCEPTION);
        }
    }

    @PostMapping("/update")
    public Result<String> updateUser(@RequestBody User user) {
        try {
            String username = userService.updateUser(user);
            return Result.success("更新成功", username);
        } catch (Exception e) {
            logger.error("更新用户信息异常:{}", e.getMessage());
            return Result.error(SYSTEM_EXCEPTION);
        }
    }

    @GetMapping("/delete/{id}")
    public Result<String> deleteUser(@PathVariable Integer id) {
        try {
            userService.deleteUser(id);
            return Result.success("删除成功");
        } catch (Exception e) {
            logger.error("删除用户异常:{}", e.getMessage());
            return Result.error(SYSTEM_EXCEPTION);
        }
    }

    @GetMapping("/recover/{id}")
    public Result<String> recoverUser(@PathVariable Integer id) {
        try {
            userService.recoverUser(id);
            return Result.success("恢复成功");
        } catch (Exception e) {
            logger.error("恢复用户异常:{}", e.getMessage());
            return Result.error(SYSTEM_EXCEPTION);
        }
    }


}
